home *** CD-ROM | disk | FTP | other *** search
/ Aminet 13 / Aminet 13 - August 1996.iso / Aminet / comm / fido / MM_AntiBomb010.lha / MM / Rexx / MM_AntiBomb.rexx < prev    next >
OS/2 REXX Batch file  |  1996-05-04  |  7KB  |  378 lines

  1. /*
  2.  
  3.                         $VER: MM_AntiBomb  0.10  (02-05-96)
  4.  
  5.                             (C) 1996 Kent B. Hansen
  6.  
  7. */
  8.  
  9. options cache
  10. options failat 99
  11. options results
  12.  
  13. signal on break_c
  14. signal on break_d
  15. signal on break_e
  16. signal on break_f
  17. signal on halt
  18. signal on ioerr
  19. signal on syntax
  20.  
  21. address 'MAILMANAGER'
  22.  
  23. Main:
  24.  
  25.     call Init
  26.     call Header
  27.     call Read_Cfg
  28.     call Check_File(system.listcmd)
  29.     call CheckInbound
  30.  
  31. call Quit(0, 'All done.')
  32. exit
  33.  
  34.  
  35. break_c:; break_d:; break_e:; break_f:; halt:
  36.  
  37.     signal off break_c
  38.     signal off break_d
  39.     signal off break_e
  40.     signal off break_f
  41.     signal off halt
  42.  
  43.     return_code        =    5
  44.     error_line    = 0
  45.     error_msg            = 'Execution halted!!!'
  46.     rc                        = 0
  47. signal Exit
  48.  
  49.  
  50. Exit:
  51.  
  52.     select
  53.         when return_code>=40 then error = 'INTERNAL-ERROR:'
  54.         when return_code>=30 then error = 'IO-ERROR:'
  55.         when return_code>=20 then error = 'ERROR:'
  56.         when return_code>=10 then error = 'WARNING:'
  57.         when return_code>=5  then error = 'INFO:'
  58.         otherwise                                    error = ''
  59.     end
  60.  
  61.   call Log()
  62.     call Log('***' strip(error error_msg) '***', '+')
  63.     call Log(,'\')
  64.  
  65.     call setclip('MM_LogPre', system.mm.logpre)
  66.  
  67. exit return_code
  68.  
  69.  
  70. Get_Version: procedure
  71.  
  72.     parse arg mode
  73.  
  74.     parse value sourceline(3-mode) with . . ver .
  75.     parse var ver tst 'ß' .
  76.  
  77.     if ~datatype(strip(tst, 'b', '/c '), 'N') then
  78.         if ~mode then ver = Get_Version(1)
  79.         else exit 99
  80.  
  81. return ver
  82.  
  83.  
  84. Header:
  85.  
  86.     call Log(,'/')
  87.     call Log('***' system.prg.id '***', '+')
  88.     call Log(' 'system.prg.cr)
  89.     call Log()
  90.  
  91. return
  92.  
  93.  
  94. Init:
  95.  
  96.     system.                        = 0
  97.  
  98.     system.prg.ver        = Get_Version(0)
  99.     system.prg.name        = 'MM_AntiBomb'
  100.     system.prg.id            = system.prg.name 'v'system.prg.ver
  101.     system.prg.cfg        = 'MM:Config/'system.prg.name'.cfg'
  102.     system.prg.cr            = '(C) 1996 Kent B. Hansen'
  103.     system.tmpfile        = 'T:'system.prg.name'.tmp'
  104.     system.mm.logpre     = getclip('MM_LogPre')
  105.     system.prg.logpre    = system.mm.logpre'|'
  106.     call                                setclip('MM_LogPre', system.prg.logpre)
  107.     MM_GetCfgPaths            'system.mm'
  108.     system.maxratio        = 85
  109.     system.listcmd        = 'c:list'
  110.  
  111.     call Include_Lib('rexxsupport')
  112. return
  113.  
  114.  
  115. Include_Lib: procedure Expose system.
  116.  
  117.     parse arg lib, prio
  118.     if right(upper(lib), 8)~='.LIBRARY' then lib    = lib'.library'
  119.     if prio=''                                                    then prio    = 0
  120.  
  121.     if ~show('l', lib) then
  122.         if ~addlib(lib, prio, -30, 0) then call Quit(20, 'Could not open' lib'!!!')
  123.  
  124. return
  125.  
  126.  
  127. Check_File: procedure expose system.
  128.  
  129.     parse arg filename
  130.  
  131.     if ~exists(filename) then call Quit(10,'Could not find' filename'!!! ***')
  132.  
  133. return
  134.  
  135.  
  136. IOerr:
  137.  
  138.     signal off ioerr
  139.  
  140.     return_code        = 20
  141.     error_line    = sigl
  142.     error_msg            = 'IO-error' rc 'at line' sigl '['errortext(rc)']')
  143.     rc                        = 0
  144. signal Exit
  145.  
  146.  
  147. Log: procedure Expose system.
  148.  
  149.     parse arg text, pre, level
  150.  
  151.     if ~datatype(level, 'N') then level = system.prg.loglevel
  152.  
  153.     tmp        = word('PRG MM', (pre~='')+1)
  154.     text    = system.tmp.logpre || pre' 'text
  155.  
  156.     MM_WriteLog 'text' level
  157. return
  158.  
  159.  
  160. Quit:
  161.  
  162.     parse arg return_code, error_msg
  163.  
  164.     error_line    = 0
  165.     rc                        = 0
  166. signal Exit
  167.  
  168.  
  169. Read_Cfg: procedure Expose system.
  170.  
  171.     MM_ReadStem system.prg.cfg 'cfg'
  172.     if RC~=0 then call Quit(31, 'Unable to read' system.prg.cfg'!!!')
  173.  
  174.     call Log('Reading config...')
  175.  
  176.     do l=0 to cfg.count-1
  177.         parse value strip(translate(cfg.l, ' ', '9'x)) with key args ';' .
  178.         key        = upper(strip(key))
  179.         args    = strip(args)
  180.  
  181.         select
  182.             when key=''                    then iterate
  183.  
  184.             when key='#PACKER'    then do
  185.                 system.packer.count    = system.packer.count + 1
  186.                 number = system.packer.count
  187.                 call CheckArgsOK(l+1,args)
  188.                 system.packer.number.name        = word(args,1)
  189.                 system.packer.number.idpos    = word(args,2)
  190.                 system.packer.number.id            = word(args,3)
  191.                 system.packer.number.tpos        = subword(args,4,2)
  192.                 system.packer.number.cmd        = subword(args,6)
  193.             end
  194.  
  195.             when key='#RATIO'        then system.maxratio = CheckRatio(args)
  196.  
  197.             when key='#LIST'        then system.listcmd = args
  198.  
  199.             when key='#DEBUG'        then system.prg.debug = 1
  200.  
  201.             otherwise say '*** CFG-ERROR: Unknown keword "'key'" at line' l+1'!!!'
  202.         end
  203.  
  204.     end
  205.  
  206. return
  207.  
  208.  
  209.  
  210. CheckArgsOK: procedure expose system.
  211.  
  212.     parse arg line, opts
  213.  
  214.     if words(opts)<6 then
  215.         call Quit(20,'Too few archiver options at line' line'!!!')
  216.  
  217.     call CheckNumber(word(opts,2))
  218.     call CheckNumber(word(opts,4))
  219.     call CheckNumber(word(opts,5))
  220.  
  221.     if word(opts,2)<1 | word(opts,2)>5 then
  222.         call Quit(20,'Illegal ID-position at line' line'!!!')
  223.  
  224.     if length(word(opts,3))+word(opts,2)>6 then
  225.         call Quit(20,'ArchiverID exceeds fifth character at line' line'!!!')
  226.  
  227.     if word(opts,4)<1 | word(opts,5)<1 then
  228.         call Quit(20,'Illegal position at line' line'!!!')
  229.  
  230.     if ~exists(word(opts,6)) then
  231.         call Quit(20,'Could not find archiver' word(opts,6)' specified at line' line'!!!')
  232.  
  233. return
  234.  
  235.  
  236.  
  237. CheckNumber: procedure expose line system.
  238.  
  239.     parse arg number
  240.  
  241.     if ~datatype(number,'n') then
  242.         call Quit(20,'Illegal archiver options at line' line'. "'number'" is not a number!!!')
  243.  
  244. return
  245.  
  246.  
  247.  
  248. CheckRatio: procedure expose system.
  249.  
  250.     parse arg ratio '.'
  251.  
  252.     if ratio<0 | ratio>100 then call Quit(20,'Illegal ratiovalue.' ratio 'not between 0 and 100!!!')
  253.  
  254. return ratio
  255.  
  256.  
  257.  
  258. Syntax:
  259.  
  260.     signal off syntax
  261.  
  262.     return_code        = 40
  263.     error_line    = sigl
  264.     error_msg            = 'Syntax-error' rc 'at line' sigl '['errortext(rc)']'
  265.     rc                        = 0
  266. signal Exit
  267.  
  268.  
  269.  
  270. Command: procedure expose system.
  271.  
  272.     parse arg cmd, log
  273.  
  274.     if log=''    then log=5
  275.  
  276.     address command cmd
  277.  
  278.     if rc>log then call Log('*** ERROR: Command "'cmd'" returned' rc'.')
  279. return rc
  280.  
  281.  
  282.  
  283. GetID: procedure expose system.
  284.  
  285.     parse arg filename
  286.  
  287.     if ~open('file',filename,'r') then call Quit(30,'Unable to read' filename'!!!')
  288.     id = ''
  289.     do i=1 to 5
  290.         id = id''readch('file')
  291.     end
  292.     if ~close('file') then call Quit(30,'Unable to close' filename'!!!')
  293.     
  294. return id
  295.  
  296.  
  297.  
  298. MatchID: procedure
  299.  
  300.     parse arg fileid, idpos, id
  301.  
  302. return substr(fileid,idpos,length(id)) = id
  303.  
  304.  
  305.  
  306. GetSize: procedure expose system.
  307.  
  308.     parse arg archive
  309.  
  310.     archiveid = GetID(archive)
  311.  
  312.     do i=1 to system.packer.count
  313.  
  314.         if MatchID(archiveid,system.packer.i.idpos,system.packer.i.id) then do
  315.  
  316.             call Command(system.packer.i.cmd' 'archive' >'system.tmpfile)
  317.             MM_ReadStem system.tmpfile 'listing'
  318.             if RC~=0 then call Quit(30,'Unable to read' system.tmpfile'!!!')
  319.             MM_DeleteFile system.tmpfile
  320.             if RC~=0 then call Quit(30,'Unable to delete' system.tmpfile'!!!')
  321.  
  322.             position = listing.count - word(system.packer.i.tpos,1)
  323.             return word(listing.position, word(system.packer.i.tpos,2))
  324.  
  325.         end
  326.     end
  327.  
  328. return -1
  329.  
  330.  
  331.  
  332. GetRatio: procedure expose system.
  333.  
  334.     parse arg filename size .
  335.  
  336.     expanded = GetSize(filename)
  337.     if expanded = -1 then return 0
  338.  
  339. return trunc(100*(expanded-size)/expanded,2)
  340.  
  341.  
  342.  
  343. CheckInbound: procedure expose system.
  344.  
  345.     wildcard='(????????|#[0-9].#[0-9].#[0-9].#[0-9]).(MO|TU|WE|TH|FR|SA|SU)([0-9]|[A-F])'
  346.     call Command(system.listcmd system.mm.inbound''wildcard' files lformat="%F%N %L %N" to' system.tmpfile)
  347.  
  348.     MM_ReadStem system.tmpfile 'archives'
  349.     if RC~=0 then call Quit(30,'Unable to read' system.tmpfile'!!!')
  350.     MM_DeleteFile system.tmpfile
  351.     if RC~=0 then call Quit(30,'Unable to delete' files.t'!!!')
  352.  
  353.     do i=0 to archives.count - 1
  354.  
  355.         ratio= GetRatio(archives.i)
  356.  
  357.         if ratio > system.maxratio then do
  358.             call Log(word(archives.i,3)' packed too hard ('ratio'%). Moved to bad!!')
  359.             MM_MoveFile word(archives.i,1) system.mm.baddir'BOMB_'word(archives.i,3)
  360.             if RC~=0 then call Quit(30,'Unable to move' words(archives.i,3)' to bad!!!')
  361.         end
  362.  
  363.         else call DebugLog(word(archives.i,3) 'OK. ('ratio'%)')
  364.     end
  365.  
  366. return
  367.  
  368.  
  369. DebugLog: procedure expose system.
  370.  
  371.  parse arg logline
  372.  
  373.  if system.prg.debug then call Log(logline)
  374.  
  375. return
  376.  
  377.  
  378.